package com.lyk.read.book.dialog

import android.view.Gravity
import android.view.LayoutInflater
import android.view.ViewGroup
import android.view.inputmethod.EditorInfo
import com.lyk.common.util.WindowUtil
import com.lyk.read.book.databinding.DialogEditBinding

/**
 * Created by LianYongKe on 2024/3/31.
 */
class EditDialog : BaseBindingDialog<DialogEditBinding>() {

    override fun getDialogWidth(): Int {
        return WindowUtil.getScreenWidth()
    }

    override fun onStart() {
        super.onStart()
        dialog?.window?.setGravity(Gravity.BOTTOM)
    }

    override fun getBinding(inflater: LayoutInflater, container: ViewGroup?): DialogEditBinding {
        return DialogEditBinding.inflate(inflater, container, false)
    }

    override fun initView() {
        arguments?.getString("data")?.let {
            binding.dialogEdit.setText(it)
        }

        binding.dialogEditConfirm.setOnClickListener {
            onConfirmCallBack?.onConfirm(binding.dialogEdit.text.toString())
            dismiss()
        }
        binding.dialogEdit.setOnEditorActionListener { textView, i, keyEvent ->
            if (i == EditorInfo.IME_ACTION_DONE) {
                onConfirmCallBack?.onConfirm(textView.text.toString())
                dismiss()
                true
            } else {
                false
            }
        }
    }
}